<template>
  <div>
    <el-table :data="tableData" border stripe>
      <el-table-column
        v-for="(col,index) in columns"
        :key="col.id"
        :prop="col.id"
        :label="col.label"
        width="150"
      >
        <template slot="header" slot-scope="scope">
          <el-row>
            <el-col :span="22" :offset="0">
              <el-input v-if="isEditMode" v-model="scope.column.label" clearable @input="colLabelInput($event,index)" />
              <div v-else>{{ scope.column.label }}</div>
            </el-col>
            <el-col :span="2" :offset="0">
              <el-button v-if="index === columns.length - 1 && isEditMode" type="text" size="default" @click="addCol">+</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  props: {
    isEditMode: {
      type: Boolean,
      default: true
    },
    columns: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      id: 0,
      tableData: [1, 2, 3, 4, 5]
    }
  },
  methods: {
    addCol() {
      this.id += 1
      this.columns.push({ id: `col_${this.id}`, label: `第${this.id}列` })
    },
    colLabelInput(e, index) {
      this.$set(this.columns[index], 'label', e)
    }
  }
}
</script>

<style>

</style>
